<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>天地图－地图API－范例－自定义覆盖物</title>
    <script type="text/javascript" src="http://api.tianditu.gov.cn/api?v=4.0&tk=您的密钥"></script>
    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
        }

        #mapDiv {
            height: 400px;
            width: 100%;
        }

        input, p {
            margin-top: 10px;
            margin-left: 5px;
            font-size: 14px;
        }
    </style>
    <script>
        var map, marker;
        var zoom = 12;
        function onLoad() {
            //初始化地图对象
            map = new T.Map("mapDiv");
            //设置显示地图的中心点和级别
            map.centerAndZoom(new T.LngLat(116.40093, 39.90313), zoom);

            var definedOverlay = T.Overlay.extend({
                initialize: function (lnglat, text, text2, options) {
                    this.lnglat = lnglat;
                    this.setOptions(options);
                    this._text = text;
                    this._overText = text2;
                },

                onAdd: function (map) {
                    this.map = map;
                    var div = this._div = document.createElement("div");
                    div.style.position = "absolute";
                    div.style.backgroundColor = "#EE5D5B";
                    div.style.border = "1px solid #BC3B3A";
                    div.style.color = "white";
                    div.style.height = "18px";
                    div.style.padding = "2px";
                    div.style.lineHeight = "18px";
                    div.style.whiteSpace = "nowrap";
                    div.style.MozUserSelect = "none";
                    div.style.fontSize = "12px";
                    var span = this._span = document.createElement("span");
                    div.appendChild(span);
                    span.appendChild(document.createTextNode(this._text));
                    var that = this;

                    var arrow = this._arrow = document.createElement("div");
                    arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
                    arrow.style.position = "absolute";
                    arrow.style.width = "11px";
                    arrow.style.height = "10px";
                    arrow.style.top = "22px";
                    arrow.style.left = "10px";
                    arrow.style.overflow = "hidden";
                    div.appendChild(arrow);

                    div.onmouseover = function () {
                        this.style.backgroundColor = "#6BADCA";
                        this.style.borderColor = "#0000ff";
                        this.getElementsByTagName("span")[0].innerHTML = that._overText;
                        arrow.style.backgroundPosition = "0px -20px";
                    }

                    div.onmouseout = function () {
                        this.style.backgroundColor = "#EE5D5B";
                        this.style.borderColor = "#BC3B3A";
                        this.getElementsByTagName("span")[0].innerHTML = that._text;
                        arrow.style.backgroundPosition = "0px 0px";
                    }

                    map.getPanes().overlayPane.appendChild(this._div);
                    this.update(this.lnglat);
                },

                onRemove: function () {
                    var parent = this.div.parentNode;
                    if (parent) {
                        parent.removeChild(this.div);
                        this.map = null;
                        this.div = null;
                    }
                },

                setLnglat: function (lnglat) {
                    this.lnglat = lnglat;
                    this.update();
                },
                getLnglat: function () {
                    return this.lnglat;
                },
                setPos: function (pos) {
                    this.lnglat = this.map.layerPointToLngLat(pos);
                    this.update();
                },
                /**
                 * 更新位置
                 */
                update: function () {
                    var pos = this.map.lngLatToLayerPoint(this.lnglat);
                    this._div.style.top = ( pos.y - 36) + "px";
                    this._div.style.left = (pos.x - 11) + "px";

                }
            });
            
            var point = new T.LngLat(116.40438, 39.87877);
            var pdefinedOverlay = new definedOverlay(point, "天坛公园", "北京市东城区天坛东路甲1号", {});
            map.addOverLay(pdefinedOverlay);
        }

    </script>
</head>
<body onLoad="onLoad()">
<div id="mapDiv"></div>
<p>本例演示自定义覆盖物</p>
</body>
</html>
